/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.biz.modular.goods.service.impl;

import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.biz.modular.cabinetdoor.entity.BizCabinetDoor;
import vip.xiaonuo.biz.modular.cabinetdoor.mapper.BizCabinetDoorMapper;
import vip.xiaonuo.biz.modular.cabinetinfo.entity.BizCabinetInfo;
import vip.xiaonuo.biz.modular.cabinetinfo.mapper.BizCabinetInfoMapper;
import vip.xiaonuo.biz.modular.goods.entity.BizGoods;
import vip.xiaonuo.biz.modular.goods.entity.BizGoodsImpItem;
import vip.xiaonuo.biz.modular.goods.mapper.BizGoodsImpItemMapper;
import vip.xiaonuo.biz.modular.goods.mapper.BizGoodsMapper;
import vip.xiaonuo.biz.modular.goods.param.GoodsImpQryDto;
import vip.xiaonuo.biz.modular.goods.param.GoodsInfoParam;
import vip.xiaonuo.biz.modular.goods.service.BizGoodsImpItemService;
import vip.xiaonuo.biz.modular.goods.service.BizGoodsService;
import vip.xiaonuo.biz.modular.org.entity.BizOrg;
import vip.xiaonuo.biz.modular.org.mapper.BizOrgMapper;
import vip.xiaonuo.biz.modular.reagentclass.entity.BizReagentClass;
import vip.xiaonuo.biz.modular.reagentclass.mapper.BizReagentClassMapper;
import vip.xiaonuo.biz.modular.user.entity.BizUser;
import vip.xiaonuo.biz.modular.user.mapper.BizUserMapper;
import vip.xiaonuo.common.util.CommonDateUtil;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 物品导入明细表Service接口实现类
 *
 * @author levin
 * @date 2023/11/06 10:38
 **/
@Service
@Transactional
public class BizGoodsImpItemServiceImpl extends ServiceImpl<BizGoodsImpItemMapper, BizGoodsImpItem> implements BizGoodsImpItemService {

    @Resource
    private BizGoodsMapper bizGoodsMapper;

    @Resource
    private BizOrgMapper bizOrgMapper;

    @Resource
    private BizCabinetInfoMapper bizCabinetInfoMapper;

    @Resource
    private BizCabinetDoorMapper bizCabinetDoorMapper;

    @Resource
    private BizUserMapper bizUserMapper;

    @Resource
    private BizGoodsService goodsService;

    @Resource
    private BizReagentClassMapper bizReagentClassMapper;


    @Override
    public List<BizGoodsImpItem> getList(GoodsImpQryDto dto) {
        QueryWrapper<BizGoodsImpItem> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(dto.getAnalFlag())) {
            queryWrapper.lambda().eq(BizGoodsImpItem::getAnalFlag, dto.getAnalFlag());
        }
        if (StringUtils.isNotBlank(dto.getImpId())) {
            queryWrapper.lambda().eq(BizGoodsImpItem::getImpId, dto.getImpId());
        }
        queryWrapper.lambda().orderByAsc(BizGoodsImpItem::getId);
        return this.list(queryWrapper);
    }

    @Override
    public void handleImpData(String impId) {
        List<BizGoodsImpItem> list = this.baseMapper.getTodoList(impId);
        for (BizGoodsImpItem item : list) {
            if (null != item) {
                if (item.getGoodsType() == 0) {
                    this.analGoods(item);
                } else {
                    this.analReagent(item);
                }
            }
        }
    }

    @Override
    public void analById(String itemId) {
        BizGoodsImpItem item = this.baseMapper.selectById(itemId);
        if (null != item) {
            if (item.getGoodsType() == 0) {
                this.analGoods(item);
            } else {
                this.analReagent(item);
            }
        }
    }

    /**
     * 解析物品
     *
     * @param itemInfo
     */
    @Async("hxunicornAsyncServiceExecutor")
    private void analGoods(BizGoodsImpItem itemInfo) {
        //当前用户
        String userId = itemInfo.getCreateUser();
        //更新信息
        BizGoodsImpItem entity = new BizGoodsImpItem();
        entity.setId(itemInfo.getId());
        //物品
        GoodsInfoParam param = new GoodsInfoParam();
        param.setGoodsType(0);
        param.setGoodsCode(itemInfo.getGoodsCode());
        param.setGoodsName(itemInfo.getGoodsName());
        param.setRfidFlag(0);
        param.setBarcodeFlag(0);
        param.setQrcodeFlag(0);
        param.setSsbrFlag(0);
        param.setAssetCode(itemInfo.getAssetCode());
        param.setGoodsModel(itemInfo.getGoodsModel());
        param.setGoodsMfr(itemInfo.getGoodsMfr());
        param.setCreateUser(itemInfo.getCreateUser());
        //校验-物品编码
        if (StringUtils.isBlank(itemInfo.getGoodsCode())) {
            entity.setAnalFlag(1);
            entity.setAnalMsg("字段【物品编码】缺失");
            this.baseMapper.updateById(entity);
            return;
        } else {
            BizGoods bizGoods = bizGoodsMapper.getInfoByCode(itemInfo.getGoodsCode());
            if (bizGoods != null) {
                if (bizGoods.getCreateUser().equals(userId)) {
                    //说明是自己的物品 后续进行更新
                    param.setId(bizGoods.getId());
                } else {
                    entity.setAnalFlag(1);
                    entity.setAnalMsg("字段【物品编码】已存在");
                    this.baseMapper.updateById(entity);
                    return;
                }
            }
        }
        //校验-物品名称
        if (StringUtils.isBlank(itemInfo.getGoodsName())) {
            entity.setAnalFlag(1);
            entity.setAnalMsg("字段【物品名称】缺失");
            this.baseMapper.updateById(entity);
            return;
        }
        //校验-rfid
        if (StringUtils.isNotBlank(itemInfo.getRfidNum())) {
            param.setRfidFlag(1);
            param.setRfidNum(itemInfo.getRfidNum());

            BizGoods bizGoods = bizGoodsMapper.getInfoByRfid(itemInfo.getRfidNum());
            if (bizGoods != null && !bizGoods.getGoodsCode().equals(itemInfo.getGoodsCode())) {
                entity.setAnalFlag(1);
                entity.setAnalMsg("字段【RFID】已存在");
                this.baseMapper.updateById(entity);
                return;
            }
        }
        //校验-是否支持条形码
        if (StringUtils.isNotBlank(itemInfo.getBarcodeFlag())) {
            if (!itemInfo.getBarcodeFlag().equals("是") && !itemInfo.getBarcodeFlag().equals("否")) {
                entity.setAnalFlag(1);
                entity.setAnalMsg("字段【是否支持条形码】内容无法识别");
                this.baseMapper.updateById(entity);
                return;
            } else {
                if (itemInfo.getBarcodeFlag().equals("是")) {
                    param.setBarcodeFlag(1);
                }
            }
        }
        //校验-是否支持二维码
        if (StringUtils.isNotBlank(itemInfo.getQrcodeFlag())) {
            if (!itemInfo.getQrcodeFlag().equals("是") && !itemInfo.getQrcodeFlag().equals("否")) {
                entity.setAnalFlag(1);
                entity.setAnalMsg("字段【是否支持二维码】内容无法识别");
                this.baseMapper.updateById(entity);
                return;
            } else {
                if (itemInfo.getQrcodeFlag().equals("是")) {
                    param.setQrcodeFlag(1);
                }
            }
        }
        //校验-支持扫码借还
        if (StringUtils.isNotBlank(itemInfo.getSsbrFlag())) {
            if (!itemInfo.getSsbrFlag().equals("是") && !itemInfo.getSsbrFlag().equals("否")) {
                entity.setAnalFlag(1);
                entity.setAnalMsg("字段【支持扫码借还】内容无法识别");
                this.baseMapper.updateById(entity);
                return;
            } else {
                if (itemInfo.getSsbrFlag().equals("是")) {
                    param.setSsbrFlag(1);
                }
            }
        }
        //校验-资产编码
        if (StringUtils.isNotBlank(itemInfo.getAssetCode())) {
            if (itemInfo.getAssetCode().length() > 200) {
                entity.setAnalFlag(1);
                entity.setAnalMsg("字段【资产编码】内容超长,请控制在100字内");
                this.baseMapper.updateById(entity);
                return;
            }
        }
        //校验-物品型号
        if (StringUtils.isNotBlank(itemInfo.getGoodsModel())) {
            if (itemInfo.getGoodsModel().length() > 200) {
                entity.setAnalFlag(1);
                entity.setAnalMsg("字段【物品型号】内容超长,请控制在100字内");
                this.baseMapper.updateById(entity);
                return;
            }
        }
        //校验-物品厂家
        if (StringUtils.isNotBlank(itemInfo.getGoodsMfr())) {
            if (itemInfo.getGoodsMfr().length() > 200) {
                entity.setAnalFlag(1);
                entity.setAnalMsg("字段【物品厂家】内容超长,请控制在100字内");
                this.baseMapper.updateById(entity);
                return;
            }
        }
        //校验-所属组织
        if (StringUtils.isNotBlank(itemInfo.getOrgName())) {
            BizOrg bizOrg = bizOrgMapper.getInfoByName(itemInfo.getOrgName());
            if (bizOrg == null) {
                entity.setAnalFlag(1);
                entity.setAnalMsg("字段【所属组织】无法匹配系统数据");
                this.baseMapper.updateById(entity);
                return;
            } else {
                param.setOrgId(bizOrg.getId());
            }
        }
        //校验-物品分类
        if (StringUtils.isNotBlank(itemInfo.getClassName())) {
            Map<String, String> dictInfo = this.baseMapper.getDictInfoByName(itemInfo.getClassName(), "GOODS_CATEGORY");
            if (dictInfo == null) {
                entity.setAnalFlag(1);
                entity.setAnalMsg("字段【物品分类】无法匹配系统数据");
                this.baseMapper.updateById(entity);
                return;
            } else {
                param.setClassId(dictInfo.get("DICT_VALUE"));
            }
        }
        //校验-存放柜子
        if (StringUtils.isNotBlank(itemInfo.getBioCabinet())) {
            BizCabinetInfo cabinetInfo = bizCabinetInfoMapper.getInfoByName(itemInfo.getBioCabinet());
            if (cabinetInfo == null) {
                entity.setAnalFlag(1);
                entity.setAnalMsg("字段【存放柜子】无法匹配系统数据");
                this.baseMapper.updateById(entity);
                return;
            } else {
                param.setBioCabinetGroupId(cabinetInfo.getCabinetGroupId());
                param.setBioCabinetId(cabinetInfo.getId());
                //校验-存放柜门
                if (StringUtils.isNotBlank(itemInfo.getBioCabinetDoor())) {
                    BizCabinetDoor cabinetDoor = bizCabinetDoorMapper.getInfoByName(itemInfo.getBioCabinetDoor(), cabinetInfo.getId());
                    if (cabinetDoor == null) {
                        entity.setAnalFlag(1);
                        entity.setAnalMsg("字段【柜门】无法匹配系统数据");
                        this.baseMapper.updateById(entity);
                        return;
                    } else {
                        param.setBioCabinetDoorId(cabinetDoor.getId());
                    }
                }
            }
        }
        //校验-管理员
        if (StringUtils.isNotBlank(itemInfo.getAdminNames())) {
            List<String> adminIds = new ArrayList<>();
            List<String> adminNameList = Arrays.asList(itemInfo.getAdminNames().split(","));
            for (String adminName : adminNameList) {
                if (adminName.indexOf("(") < 0 || adminName.indexOf(")") < 0) {
                    entity.setAnalFlag(1);
                    entity.setAnalMsg("字段【物品管理员】【" + adminName + "】格式不正确");
                    this.baseMapper.updateById(entity);
                    return;
                }
                //获取账号
                String account = adminName.substring(adminName.indexOf("(") + 1, adminName.indexOf(")"));
                String name = adminName.substring(0, adminName.indexOf("("));
                BizUser bizUser = bizUserMapper.getUserInfoByAccount(account);
                //判断长度不对情况下
                if (adminName.indexOf(")") + 1 != adminName.length()) {
                    entity.setAnalFlag(1);
                    entity.setAnalMsg("字段【物品管理员】【" + adminName + "】格式错误");
                    this.baseMapper.updateById(entity);
                    return;
                }
                if (bizUser == null) {
                    entity.setAnalFlag(1);
                    entity.setAnalMsg("字段【物品管理员】【" + adminName + "】无法匹配系统数据");
                    this.baseMapper.updateById(entity);
                    return;
                } else {
                    adminIds.add(bizUser.getId());
                    if (!name.equals(bizUser.getName())) {
                        entity.setAnalFlag(1);
                        entity.setAnalMsg("字段【物品管理员】【" + adminName + "】姓名与账号无法匹配");
                        this.baseMapper.updateById(entity);
                        return;
                    }
                }
            }
            param.setAdminUserIds(String.join(",", adminIds));
        }
        //校验-物品标签
        if (StringUtils.isNotBlank(itemInfo.getGoodsLabels())) {
            List<String> labelIds = new ArrayList<>();
            List<String> labelList = Arrays.asList(itemInfo.getGoodsLabels().split(","));
            for (String label : labelList) {
                Map<String, String> dictInfo = this.baseMapper.getDictInfoByName(label, "GOODS_LABEL");
                if (dictInfo == null) {
                    entity.setAnalFlag(1);
                    entity.setAnalMsg("字段【物品标签】【" + label + "】无法匹配系统数据");
                    this.baseMapper.updateById(entity);
                    return;
                } else {
                    labelIds.add(dictInfo.get("DICT_VALUE"));
                }
            }
            param.setLabels(labelIds);
        }
        //校验-最长离柜时限
        if (StringUtils.isNotBlank(itemInfo.getMaxLimitLeave())) {
            if (itemInfo.getMaxLimitLeave().length() > 6) {
                entity.setAnalFlag(1);
                entity.setAnalMsg("字段【最长离柜时限】内容超长,最大位100000");
                this.baseMapper.updateById(entity);
                return;
            }
            if (NumberUtil.isInteger(itemInfo.getMaxLimitLeave())) {
                param.setMaxLimitLeave(Integer.valueOf(itemInfo.getMaxLimitLeave()));
                param.setMaxLimitLeaveUnit(1);
                if (Integer.valueOf(itemInfo.getMaxLimitLeave()) <= 0) {
                    entity.setAnalFlag(1);
                    entity.setAnalMsg("字段【最长离柜时限】必须为大于0的整数");
                    this.baseMapper.updateById(entity);
                    return;
                }
                if (Integer.valueOf(itemInfo.getMaxLimitLeave()) > 100000) {
                    entity.setAnalFlag(1);
                    entity.setAnalMsg("字段【最长离柜时限】必须小于等于100000");
                    this.baseMapper.updateById(entity);
                    return;
                }
            } else {
                entity.setAnalFlag(1);
                entity.setAnalMsg("字段【最长离柜时限】非整数");
                this.baseMapper.updateById(entity);
                return;
            }
        }
        //校验-到期前提示
        if (StringUtils.isNotBlank(itemInfo.getImpendPrompt())) {
            if (itemInfo.getImpendPrompt().length() > 6) {
                entity.setAnalFlag(1);
                entity.setAnalMsg("字段【到期前提示】内容超长,最大位100000");
                this.baseMapper.updateById(entity);
                return;
            }
            if (NumberUtil.isInteger(itemInfo.getImpendPrompt())) {
                param.setImpendPrompt(Integer.valueOf(itemInfo.getImpendPrompt()));
                param.setImpendPromptUnit(1);
                if (Integer.valueOf(itemInfo.getImpendPrompt()) <= 0) {
                    entity.setAnalFlag(1);
                    entity.setAnalMsg("字段【到期前提示】必须为大于0的整数");
                    this.baseMapper.updateById(entity);
                    return;
                }
                if (Integer.valueOf(itemInfo.getImpendPrompt()) > 100000) {
                    entity.setAnalFlag(1);
                    entity.setAnalMsg("字段【到期前提示】必须小于等于100000");
                    this.baseMapper.updateById(entity);
                    return;
                }
            } else {
                entity.setAnalFlag(1);
                entity.setAnalMsg("字段【到期前提示】非整数");
                this.baseMapper.updateById(entity);
                return;
            }
        }
        //校验-连续发送预警时间间隔
        if (StringUtils.isNotBlank(itemInfo.getAlertInterval())) {
            if (itemInfo.getAlertInterval().length() > 6) {
                entity.setAnalFlag(1);
                entity.setAnalMsg("字段【连续发送预警时间间隔】内容超长,最大位100000");
                this.baseMapper.updateById(entity);
                return;
            }
            if (NumberUtil.isInteger(itemInfo.getAlertInterval())) {
                param.setAlertInterval(Integer.valueOf(itemInfo.getAlertInterval()));
                param.setAlertIntervalUnit(1);
                if (Integer.valueOf(itemInfo.getAlertInterval()) <= 0) {
                    entity.setAnalFlag(1);
                    entity.setAnalMsg("字段【连续发送预警时间间隔】必须为大于0的整数");
                    this.baseMapper.updateById(entity);
                    return;
                }
                if (Integer.valueOf(itemInfo.getAlertInterval()) > 100000) {
                    entity.setAnalFlag(1);
                    entity.setAnalMsg("字段【连续发送预警时间间隔】必须小于等于100000");
                    this.baseMapper.updateById(entity);
                    return;
                }
            } else {
                entity.setAnalFlag(1);
                entity.setAnalMsg("字段【连续发送预警时间间隔】非整数");
                this.baseMapper.updateById(entity);
                return;
            }
        }
        //校验-连续发送预警最大次数
        if (StringUtils.isNotBlank(itemInfo.getAlertMaxNum())) {
            if (itemInfo.getAlertMaxNum().length() > 6) {
                entity.setAnalFlag(1);
                entity.setAnalMsg("字段【连续发送预警最大次数】内容超长,最大位100000");
                this.baseMapper.updateById(entity);
                return;
            }
            if (NumberUtil.isInteger(itemInfo.getAlertMaxNum())) {
                param.setAlertMaxNum(Integer.valueOf(itemInfo.getAlertMaxNum()));
                if (Integer.valueOf(itemInfo.getAlertMaxNum()) <= 0) {
                    entity.setAnalFlag(1);
                    entity.setAnalMsg("字段【连续发送预警最大次数】必须为大于0的整数");
                    this.baseMapper.updateById(entity);
                    return;
                }
                if (Integer.valueOf(itemInfo.getAlertMaxNum()) > 100000) {
                    entity.setAnalFlag(1);
                    entity.setAnalMsg("字段【连续发送预警最大次数】必须小于等于100000");
                    this.baseMapper.updateById(entity);
                    return;
                }
            } else {
                entity.setAnalFlag(1);
                entity.setAnalMsg("字段【连续发送预警最大次数】非整数");
                this.baseMapper.updateById(entity);
                return;
            }
        }
        //校验-物品描述
        if (StringUtils.isNotBlank(itemInfo.getGoodsDes())) {
            if (itemInfo.getGoodsDes().length() > 400) {
                entity.setAnalFlag(1);
                entity.setAnalMsg("字段【物品描述】内容超长,请控制在200字内");
                this.baseMapper.updateById(entity);
                return;
            } else {
                param.setGoodsDes(itemInfo.getGoodsDes());
            }
        }
        //校验-注意事项
        if (StringUtils.isNotBlank(itemInfo.getAttention())) {
            if (itemInfo.getAttention().length() > 400) {
                entity.setAnalFlag(1);
                entity.setAnalMsg("字段【注意事项】内容超长,请控制在200字内");
                this.baseMapper.updateById(entity);
                return;
            } else {
                param.setAttention(itemInfo.getAttention());
            }
        }
        //校验-备注
        if (StringUtils.isNotBlank(itemInfo.getRemarks())) {
            if (itemInfo.getRemarks().length() > 400) {
                entity.setAnalFlag(1);
                entity.setAnalMsg("字段【备注】内容超长,请控制在200字内");
                this.baseMapper.updateById(entity);
                return;
            } else {
                param.setRemarks(itemInfo.getRemarks());
            }
        }
        //处理物品的插入or更新
        if (StringUtils.isNotBlank(param.getId())) {
            goodsService.edit(param);
        } else {
            String goodsId = IdWorker.getIdStr();
            param.setId(goodsId);
            goodsService.add(param);
        }
        entity.setAnalFlag(2);
        entity.setGoodsId(param.getId());
        this.baseMapper.updateById(entity);
    }

    /**
     * 解析试剂
     *
     * @param itemInfo
     */
    @Async("hxunicornAsyncServiceExecutor")
    private void analReagent(BizGoodsImpItem itemInfo) {
        //当前用户
        String userId = itemInfo.getCreateUser();
        //更新信息
        BizGoodsImpItem entity = new BizGoodsImpItem();
        entity.setId(itemInfo.getId());
        //物品
        GoodsInfoParam param = new GoodsInfoParam();
        param.setGoodsType(1);
        param.setGoodsName(itemInfo.getGoodsName());
        param.setRfidFlag(0);
        param.setBarcodeFlag(0);
        param.setQrcodeFlag(0);
        param.setSsbrFlag(0);
        param.setReagentWeighNeedFlag("1");
        param.setCreateUser(itemInfo.getCreateUser());

        //校验-物品名称
        if (StringUtils.isBlank(itemInfo.getGoodsName())) {
            entity.setAnalFlag(1);
            entity.setAnalMsg("字段【物品名称】缺失");
            this.baseMapper.updateById(entity);
            return;
        } else {
            BizReagentClass bizReagentClass = bizReagentClassMapper.getInfoByName(itemInfo.getGoodsName());
            if (bizReagentClass == null) {
                entity.setAnalFlag(1);
                entity.setAnalMsg("字段【物品名称】【" + itemInfo.getGoodsName() + "】无法匹配系统数据");
                this.baseMapper.updateById(entity);
                return;
            } else {
                param.setGoodsName(bizReagentClass.getReagentName());
                param.setBizReagentClassId(bizReagentClass.getId());
                param.setReagentCasNo(bizReagentClass.getCasNo());
                param.setReagentClassId(bizReagentClass.getClassId());
                param.setReagentFormId(bizReagentClass.getFormId());
                param.setReagentDensity(bizReagentClass.getDensity());
                param.setReagentUnit(bizReagentClass.getUnit());
                param.setReagentRedoxClassId(bizReagentClass.getRedoxClassId());
                param.setReagentBottleType(bizReagentClass.getBottleType());
                param.setMaxLimitLeaveUnit(1);
                if (StringUtils.isNotBlank(bizReagentClass.getMaxLimitLeave())) {
                    param.setMaxLimitLeave(Integer.valueOf(bizReagentClass.getMaxLimitLeave()));
                }
                param.setReagentWeighNeedFlag(bizReagentClass.getWeighNeedFlag());
                param.setReagentMaxErrorValue(bizReagentClass.getMaxErrorValue());
            }
        }
        //校验-rfid
        if (StringUtils.isNotBlank(itemInfo.getRfidNum())) {
            param.setRfidFlag(1);
            param.setRfidNum(itemInfo.getRfidNum());

            BizGoods bizGoods = bizGoodsMapper.getInfoByRfid(itemInfo.getRfidNum());
            if (bizGoods != null) {
                entity.setAnalFlag(1);
                entity.setAnalMsg("字段【RFID】已存在");
                this.baseMapper.updateById(entity);
                return;
            }
        }
        //校验-是否支持条形码
        if (StringUtils.isNotBlank(itemInfo.getBarcodeFlag())) {
            if (!itemInfo.getBarcodeFlag().equals("是") && !itemInfo.getBarcodeFlag().equals("否")) {
                entity.setAnalFlag(1);
                entity.setAnalMsg("字段【是否支持条形码】内容无法识别");
                this.baseMapper.updateById(entity);
                return;
            } else {
                if (itemInfo.getBarcodeFlag().equals("是")) {
                    param.setBarcodeFlag(1);
                }
            }
        }
        //校验-是否支持二维码
        if (StringUtils.isNotBlank(itemInfo.getQrcodeFlag())) {
            if (!itemInfo.getQrcodeFlag().equals("是") && !itemInfo.getQrcodeFlag().equals("否")) {
                entity.setAnalFlag(1);
                entity.setAnalMsg("字段【是否支持二维码】内容无法识别");
                this.baseMapper.updateById(entity);
                return;
            } else {
                if (itemInfo.getQrcodeFlag().equals("是")) {
                    param.setQrcodeFlag(1);
                }
            }
        }
        //校验-支持扫码借还
        if (StringUtils.isNotBlank(itemInfo.getSsbrFlag())) {
            if (!itemInfo.getSsbrFlag().equals("是") && !itemInfo.getSsbrFlag().equals("否")) {
                entity.setAnalFlag(1);
                entity.setAnalMsg("字段【支持扫码借还】内容无法识别");
                this.baseMapper.updateById(entity);
                return;
            } else {
                if (itemInfo.getSsbrFlag().equals("是")) {
                    param.setSsbrFlag(1);
                }
            }
        }
        //校验-初始净量
        if (StringUtils.isNotBlank(itemInfo.getReagentInitNetAmount())) {
            if (NumberUtil.isInteger(itemInfo.getReagentInitNetAmount())) {
                param.setReagentInitNetAmount(new BigDecimal(itemInfo.getReagentInitNetAmount()));
                if (Integer.valueOf(itemInfo.getReagentInitNetAmount()) <= 0) {
                    entity.setAnalFlag(1);
                    entity.setAnalMsg("字段【初始净量】必须为大于0的整数");
                    this.baseMapper.updateById(entity);
                    return;
                }
            } else {
                entity.setAnalFlag(1);
                entity.setAnalMsg("字段【初始净量】非整数");
                this.baseMapper.updateById(entity);
                return;
            }
        } else {
            entity.setAnalFlag(1);
            entity.setAnalMsg("字段【初始净量】缺失");
            this.baseMapper.updateById(entity);
            return;
        }
        //校验-试剂单位
        if (StringUtils.isNotBlank(itemInfo.getReagentUnit())) {
            if (!itemInfo.getReagentUnit().equals("g") && !itemInfo.getReagentUnit().equals("ml")) {
                entity.setAnalFlag(1);
                entity.setAnalMsg("字段【试剂单位】内容无法识别");
                this.baseMapper.updateById(entity);
                return;
            } else {
                if (itemInfo.getReagentUnit().equals("g")) {
                    param.setReagentUnit("1");
                } else {
                    param.setReagentUnit("0");
                }
            }
        } else {
            entity.setAnalFlag(1);
            entity.setAnalMsg("字段【试剂单位】缺失");
            this.baseMapper.updateById(entity);
            return;
        }
        //校验-有效期
        if (StringUtils.isNotBlank(itemInfo.getReagentValidityPeriod())) {
            if (!CommonDateUtil.isValidDate(itemInfo.getReagentValidityPeriod(), "yyyy-MM-dd")) {
                entity.setAnalFlag(1);
                entity.setAnalMsg("字段【有效期】【" + itemInfo.getReagentValidityPeriod() + "】格式无法识别");
                this.baseMapper.updateById(entity);
                return;
            } else {
                param.setReagentValidityPeriod(itemInfo.getReagentValidityPeriod());
            }
        }
        //校验-存放柜子
        if (StringUtils.isNotBlank(itemInfo.getBioCabinet())) {
            BizCabinetInfo cabinetInfo = bizCabinetInfoMapper.getInfoByName(itemInfo.getBioCabinet());
            if (cabinetInfo == null) {
                entity.setAnalFlag(1);
                entity.setAnalMsg("字段【存放柜子】无法匹配系统数据");
                this.baseMapper.updateById(entity);
                return;
            } else {
                param.setBioCabinetGroupId(cabinetInfo.getCabinetGroupId());
                param.setBioCabinetId(cabinetInfo.getId());
                //校验-存放柜门
                if (StringUtils.isNotBlank(itemInfo.getBioCabinetDoor())) {
                    BizCabinetDoor cabinetDoor = bizCabinetDoorMapper.getInfoByName(itemInfo.getBioCabinetDoor(), cabinetInfo.getId());
                    if (cabinetDoor == null) {
                        entity.setAnalFlag(1);
                        entity.setAnalMsg("字段【柜门】无法匹配系统数据");
                        this.baseMapper.updateById(entity);
                        return;
                    } else {
                        param.setBioCabinetDoorId(cabinetDoor.getId());
                    }
                }
            }
        }
        //校验-取还是否需要称重
        if (StringUtils.isNotBlank(itemInfo.getReagentWeighNeedFlag())) {
            if (!itemInfo.getReagentWeighNeedFlag().equals("是") && !itemInfo.getReagentWeighNeedFlag().equals("否")) {
                entity.setAnalFlag(1);
                entity.setAnalMsg("字段【取还是否需要称重】内容无法识别");
                this.baseMapper.updateById(entity);
                return;
            } else {
                if (itemInfo.getReagentWeighNeedFlag().equals("是")) {
                    param.setReagentWeighNeedFlag("1");
                } else {
                    param.setReagentWeighNeedFlag("0");
                }
            }
        }
        //校验-试剂重量误差最大值
        if (StringUtils.isNotBlank(itemInfo.getReagentMaxErrorValue())) {
            if (itemInfo.getReagentMaxErrorValue().length() > 6) {
                entity.setAnalFlag(1);
                entity.setAnalMsg("字段【试剂重量误差最大值】内容超长,最大位100000");
                this.baseMapper.updateById(entity);
                return;
            }
            if (NumberUtil.isInteger(itemInfo.getReagentMaxErrorValue())) {
                param.setReagentMaxErrorValue(new BigDecimal(itemInfo.getReagentMaxErrorValue()));
                if (Integer.valueOf(itemInfo.getReagentMaxErrorValue()) <= 0) {
                    entity.setAnalFlag(1);
                    entity.setAnalMsg("字段【试剂重量误差最大值】必须为大于0的整数");
                    this.baseMapper.updateById(entity);
                    return;
                }
            } else {
                entity.setAnalFlag(1);
                entity.setAnalMsg("字段【试剂重量误差最大值】非整数");
                this.baseMapper.updateById(entity);
                return;
            }
        }
        //校验-最长离柜时限
        if (StringUtils.isNotBlank(itemInfo.getMaxLimitLeave())) {
            if (itemInfo.getMaxLimitLeave().length() > 6) {
                entity.setAnalFlag(1);
                entity.setAnalMsg("字段【最长离柜时限】内容超长,最大位100000");
                this.baseMapper.updateById(entity);
                return;
            }
            if (NumberUtil.isInteger(itemInfo.getMaxLimitLeave())) {
                param.setMaxLimitLeave(Integer.valueOf(itemInfo.getMaxLimitLeave()));
                param.setMaxLimitLeaveUnit(1);
                if (Integer.valueOf(itemInfo.getMaxLimitLeave()) <= 0) {
                    entity.setAnalFlag(1);
                    entity.setAnalMsg("字段【最长离柜时限】必须为大于0的整数");
                    this.baseMapper.updateById(entity);
                    return;
                }
                if (Integer.valueOf(itemInfo.getMaxLimitLeave()) > 100000) {
                    entity.setAnalFlag(1);
                    entity.setAnalMsg("字段【最长离柜时限】必须小于等于100000");
                    this.baseMapper.updateById(entity);
                    return;
                }
            } else {
                entity.setAnalFlag(1);
                entity.setAnalMsg("字段【最长离柜时限】非整数");
                this.baseMapper.updateById(entity);
                return;
            }
        }
        //校验-到期前提示
        if (StringUtils.isNotBlank(itemInfo.getImpendPrompt())) {
            if (itemInfo.getImpendPrompt().length() > 6) {
                entity.setAnalFlag(1);
                entity.setAnalMsg("字段【到期前提示】内容超长,最大位100000");
                this.baseMapper.updateById(entity);
                return;
            }
            if (NumberUtil.isInteger(itemInfo.getImpendPrompt())) {
                param.setImpendPrompt(Integer.valueOf(itemInfo.getImpendPrompt()));
                param.setImpendPromptUnit(1);
                if (Integer.valueOf(itemInfo.getImpendPrompt()) <= 0) {
                    entity.setAnalFlag(1);
                    entity.setAnalMsg("字段【到期前提示】必须为大于0的整数");
                    this.baseMapper.updateById(entity);
                    return;
                }
                if (Integer.valueOf(itemInfo.getImpendPrompt()) > 100000) {
                    entity.setAnalFlag(1);
                    entity.setAnalMsg("字段【到期前提示】必须小于等于100000");
                    this.baseMapper.updateById(entity);
                    return;
                }
            } else {
                entity.setAnalFlag(1);
                entity.setAnalMsg("字段【到期前提示】非整数");
                this.baseMapper.updateById(entity);
                return;
            }
        }
        //校验-连续发送预警时间间隔
        if (StringUtils.isNotBlank(itemInfo.getAlertInterval())) {
            if (itemInfo.getAlertInterval().length() > 6) {
                entity.setAnalFlag(1);
                entity.setAnalMsg("字段【连续发送预警时间间隔】内容超长,最大位100000");
                this.baseMapper.updateById(entity);
                return;
            }
            if (NumberUtil.isInteger(itemInfo.getAlertInterval())) {
                param.setAlertInterval(Integer.valueOf(itemInfo.getAlertInterval()));
                param.setAlertIntervalUnit(1);
                if (Integer.valueOf(itemInfo.getAlertInterval()) <= 0) {
                    entity.setAnalFlag(1);
                    entity.setAnalMsg("字段【连续发送预警时间间隔】必须为大于0的整数");
                    this.baseMapper.updateById(entity);
                    return;
                }
                if (Integer.valueOf(itemInfo.getAlertInterval()) > 100000) {
                    entity.setAnalFlag(1);
                    entity.setAnalMsg("字段【连续发送预警时间间隔】必须小于等于100000");
                    this.baseMapper.updateById(entity);
                    return;
                }
            } else {
                entity.setAnalFlag(1);
                entity.setAnalMsg("字段【连续发送预警时间间隔】非整数");
                this.baseMapper.updateById(entity);
                return;
            }
        }
        //校验-连续发送预警最大次数
        if (StringUtils.isNotBlank(itemInfo.getAlertMaxNum())) {
            if (itemInfo.getAlertMaxNum().length() > 6) {
                entity.setAnalFlag(1);
                entity.setAnalMsg("字段【连续发送预警最大次数】内容超长,最大位100000");
                this.baseMapper.updateById(entity);
                return;
            }
            if (NumberUtil.isInteger(itemInfo.getAlertMaxNum())) {
                param.setAlertMaxNum(Integer.valueOf(itemInfo.getAlertMaxNum()));
                if (Integer.valueOf(itemInfo.getAlertMaxNum()) <= 0) {
                    entity.setAnalFlag(1);
                    entity.setAnalMsg("字段【连续发送预警最大次数】必须为大于0的整数");
                    this.baseMapper.updateById(entity);
                    return;
                }
                if (Integer.valueOf(itemInfo.getAlertMaxNum()) > 100000) {
                    entity.setAnalFlag(1);
                    entity.setAnalMsg("字段【连续发送预警最大次数】必须小于等于100000");
                    this.baseMapper.updateById(entity);
                    return;
                }
            } else {
                entity.setAnalFlag(1);
                entity.setAnalMsg("字段【连续发送预警最大次数】非整数");
                this.baseMapper.updateById(entity);
                return;
            }
        }
        //校验-购买者
        if (StringUtils.isNotBlank(itemInfo.getReagentBuyerName())) {
            if (itemInfo.getReagentBuyerName().length() > 200) {
                entity.setAnalFlag(1);
                entity.setAnalMsg("字段【购买者】内容超长,请控制在100字内");
                this.baseMapper.updateById(entity);
                return;
            } else {
                param.setReagentBuyerName(itemInfo.getReagentBuyerName());
            }
        }
        //校验-供应商
        if (StringUtils.isNotBlank(itemInfo.getReagentSupplier())) {
            if (itemInfo.getReagentSupplier().length() > 200) {
                entity.setAnalFlag(1);
                entity.setAnalMsg("字段【供应商】内容超长,请控制在100字内");
                this.baseMapper.updateById(entity);
                return;
            } else {
                param.setReagentSupplier(itemInfo.getReagentSupplier());
            }
        }
        //校验-购买凭证
        if (StringUtils.isNotBlank(itemInfo.getReagentPurchaseVoucher())) {
            if (itemInfo.getReagentPurchaseVoucher().length() > 200) {
                entity.setAnalFlag(1);
                entity.setAnalMsg("字段【购买凭证】内容超长,请控制在100字内");
                this.baseMapper.updateById(entity);
                return;
            } else {
                param.setReagentPurchaseVoucher(itemInfo.getReagentPurchaseVoucher());
            }
        }
        //校验-备注
        if (StringUtils.isNotBlank(itemInfo.getRemarks())) {
            if (itemInfo.getRemarks().length() > 400) {
                entity.setAnalFlag(1);
                entity.setAnalMsg("字段【备注】内容超长,请控制在200字内");
                this.baseMapper.updateById(entity);
                return;
            } else {
                param.setRemarks(itemInfo.getRemarks());
            }
        }
        String goodsId = IdWorker.getIdStr();
        param.setId(goodsId);
        goodsService.add(param);

        entity.setAnalFlag(2);
        entity.setGoodsId(param.getId());
        this.baseMapper.updateById(entity);
    }
}